package com.zzrc.grape.service.impl;

import com.zzrc.grape.service.OrderService;
import com.zzrc.grape.util.JDBCUtil;
import com.zzrc.peony.dao.mapper.*;
import com.zzrc.peony.model.db.*;
import com.zzrc.sdk.whale.util.SdkBizUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private RecordLoanMapper recordLoanMapper;
    @Resource
    private RecordRepaymentMapper recordRepaymentMapper;
    @Resource
    private RecordRepaymentPlanMapper recordRepaymentPlanMapper;
    @Resource
    private TransWithdrawMapper transWithdrawMapper;
    @Resource
    private TransDeductionMapper transDeductionMapper;

    @Override
    public Boolean changeOrderStatus(String loanGid, String env, String operation) {
        int line = 0;
        if ("流标".equals(operation)) {
            line = updateOrderStatusByLoanGid(loanGid, 7);
        } else if ("删除订单".equals(operation)) {
            line = updateOrderUserGidByLoanGid(loanGid, "e48030aef25e4d94be4d47b4efa12345");
        } else if ("结清".equals(operation)) {
            line = updateOrderStatusByLoanGid(loanGid, 10);
        } else if ("待签署协议".equals(operation)) {
            line = updateOrderStatusByLoanGid(loanGid, 4);
        } else if ("待还款".equals(operation)) {
            line = updateOrderStatusByLoanGid(loanGid, 9);
        } else if ("修改借款时间".equals(operation)) {
            line = 1;
        }
        return line > 0;
    }

    @Override
    public List<RecordLoan> getRecordLoanList(String loanGid) {
        if (SdkBizUtil.isEmpty(loanGid)) {
            return null;
        }
        RecordLoanExample example = new RecordLoanExample();
        RecordLoanExample.Criteria criteria = example.createCriteria();
        criteria.andGidEqualTo(loanGid);
        return recordLoanMapper.selectByExample(example);
    }

    @Override
    public List<RecordRepayment> getRecordRepaymentList(String loanGid) {
        if (SdkBizUtil.isEmpty(loanGid)) {
            return null;
        }
        RecordRepaymentExample example = new RecordRepaymentExample();
        RecordRepaymentExample.Criteria criteria = example.createCriteria();
        criteria.andLoanGidEqualTo(loanGid);
        return recordRepaymentMapper.selectByExample(example);
    }

    @Override
    public List<RecordRepaymentPlan> getRecordRepaymentPlanList(String loanGid) {
        if (SdkBizUtil.isEmpty(loanGid)) {
            return null;
        }
        RecordRepaymentPlanExample example = new RecordRepaymentPlanExample();
        RecordRepaymentPlanExample.Criteria criteria = example.createCriteria();
        criteria.andLoanGidEqualTo(loanGid);
        return recordRepaymentPlanMapper.selectByExample(example);
    }

    @Override
    public List<TransWithdraw> getTransWithdrawList(String loanGid) {
        if (SdkBizUtil.isEmpty(loanGid)) {
            return null;
        }
        TransWithdrawExample example = new TransWithdrawExample();
        TransWithdrawExample.Criteria criteria = example.createCriteria();
        criteria.andLoanGidEqualTo(loanGid);
        return transWithdrawMapper.selectByExample(example);
    }

    @Override
    public List<TransDeduction> getTransDeductionList(String loanGid) {
        if (SdkBizUtil.isEmpty(loanGid)) {
            return null;
        }
        RecordLoanExample recordLoanExample = new RecordLoanExample();
        RecordLoanExample.Criteria recordLoanCriteria = recordLoanExample.createCriteria();
        recordLoanCriteria.andGidEqualTo(loanGid);
        List<RecordLoan> recordLoans = recordLoanMapper.selectByExample(recordLoanExample);
        if (recordLoans.size() == 0) {
            return null;
        }
        String userGid = recordLoans.get(0).getUserGid();
        TransDeductionExample example = new TransDeductionExample();
        TransDeductionExample.Criteria criteria = example.createCriteria();
        criteria.andUserGidEqualTo(userGid);
        return transDeductionMapper.selectByExample(example);
    }

    public int updateOrderStatusByLoanGid(String loanGid, int status) {
        try {
            Connection conn = JDBCUtil.getConnection();
            String sql = "update peony.record_loan set status = ? where gid = ?";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setInt(1, status);
            statement.setString(2, loanGid);
            return statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public int updateOrderUserGidByLoanGid(String loanGid, String userGid) {
        try {
            Connection conn = JDBCUtil.getConnection();
            String sql = "update peony.record_loan set user_gid = ? where gid = ?";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, userGid);
            statement.setString(2, loanGid);
            return statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

}
